package com.whfc.common.result;

import java.io.Serializable;

public class R<T> implements Serializable {

    /**
     * 状态码
     */
    private String code;

    /**
     * 描述信息
     */
    private String msg;

    /**
     * 返回数据
     */
    private T data;

    public R() {
    }

    public R(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    private static R result(ResultEnum resultEnum) {
        return result(resultEnum, null);
    }

    private static R result(ResultEnum resultEnum, Object data) {
        R res = new R(resultEnum.getCode(), resultEnum.getMessage());
        res.setData(data);
        return res;
    }

    public static R success() {
        return result(ResultEnum.SUCCESS);
    }

    public static R success(Object data) {
        return result(ResultEnum.SUCCESS, data);
    }

    public static R failure() {
        return result(ResultEnum.FAILURE);
    }

    public static R failure(Object data) {
        return result(ResultEnum.FAILURE, data);
    }

    public static R paramError() {
        return result(ResultEnum.PARAM_ERROR);
    }

    public static R paramError(Object data) {
        return result(ResultEnum.PARAM_ERROR, data);
    }

    public static R authError() {
        return result(ResultEnum.AUTHZ_ERROR);
    }

    public static R authError(Object data) {
        return result(ResultEnum.AUTHZ_ERROR, data);
    }

    public static R tokenExpired() {
        return result(ResultEnum.TOKEN_EXPIRED);
    }

    public static R unknownError() {
        return result(ResultEnum.UNKNOWN_ERROR);
    }

    public static R unknownError(Object data) {
        return result(ResultEnum.UNKNOWN_ERROR, data);
    }

    @Override
    public String toString() {
        return "R{" +
                "code='" + code + '\'' +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
